/*
* Tank.java
* Copyright (c) 2007 Aris Bartee
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* July 10, 2007
*/
package afterburner.jsp.fuel;

import javax.servlet.ServletContext;
import afterburner.jsp.Base;
import java.util.ArrayList;
import java.util.List;

/**
* Fuel Additive call back manager.
* @author arisbartee
*/
public class Tank {
	public final static String FUEL_ADDITIVES = "afterburner.fuel_additives";

	
	
	public static void addFuels(ServletContext context){
                String additives_list = context.getInitParameter(FUEL_ADDITIVES);
                context.log(additives_list);
                String additives [] = new String[0];
                if(additives_list != null)additives= additives_list.split(",");
                List<FuelAdditive> list = new ArrayList<FuelAdditive>();
               
                for(String additive_cn:additives){
                    try{
                        Class clazz = Class.forName(additive_cn);
                        FuelAdditive fa = (FuelAdditive)clazz.newInstance();
						fa.contextInitialized(context);
                        list.add(fa);
                    }catch(Exception e){
                        context.log(additive_cn + " not added");
                    }
                }
                 list.add(new ActionAdditive());
                list.add(new LayoutAdditive());
		FuelAdditive fas[] = (FuelAdditive [])list.toArray(new FuelAdditive[list.size()]);
		context.setAttribute(FUEL_ADDITIVES,fas);
	}
	
	public static void removeFuels(ServletContext context){
			FuelAdditive fas []= (FuelAdditive [])context.getAttribute(FUEL_ADDITIVES);
			for(FuelAdditive fa : fas){

				fa.contextDestroyed(context);
			}
		context.removeAttribute(FUEL_ADDITIVES);
	}
	
	public static void activateFuel(Base baseJsp){
		ServletContext context = baseJsp.getServletConfig().getServletContext();
		FuelAdditive fas []= (FuelAdditive [])context.getAttribute(FUEL_ADDITIVES);
		for(FuelAdditive fa : fas){
			
			fa.preJSP(baseJsp);
		}
	}
	
	public static void deActivateFuel(Base baseJsp){
		ServletContext context = baseJsp.getServletConfig().getServletContext();
		FuelAdditive fas []= (FuelAdditive [])context.getAttribute(FUEL_ADDITIVES);
		for(FuelAdditive fa : fas){
			fa.postJSP(baseJsp);
		}
	}
}
